﻿<?php include_once("connection.php"); // the connection to the database ?>
<?php if(!$_GET['submitted']) { ?>
	<div style="overflow:hidden; margin:1em;">
		<h2>Bạn có thật sự muốn xóa "<?php echo $_GET['cat_ten'] ?>" và các nhánh con?</h2>
		<a href="/application/models/ajax/category/delete.php?cat_parentID=<?php echo $_GET['cat_parentID'] ?>&cat_ten=<?php echo $_GET['cat_ten'] ?>&submitted=true" class="nyroModal">[ <?php echo YES; ?> ]</a>
		<a href="" onclick="$.nyroModalRemove();">[ <?php echo NO; ?> ]</a>
	</div>
<?php } else { 
	// recursive delete function
	function cascadeDelete($cat_id, $conn) {
		$relatedCategory =  mysql_query("SELECT cat_id FROM category WHERE cat_parentID = ".$cat_id) or die(mysql_error());
		mysql_query("DELETE FROM category WHERE cat_id = ".$cat_id) or die(mysql_error());
		while ($row = mysql_fetch_assoc($relatedCategory)) {
			$this_id =  $row["cat_id"];
			cascadeDelete($this_id, $conn);
			$relatedCategory->MoveNext();
		}
	}
	cascadeDelete($_GET['cat_parentID'],$conn);
	?>
	<div style="overflow:hidden; margin:1em;">
		<h1>Branch deleted!</h1>
		<a href="#" onclick="$.nyroModalRemove();">Close</a>
	</div>
<?php } ?>